package hu.gar.core.dao.impl;

import hu.gar.core.dao.UserDao;
import hu.gar.core.entity.User;
import hu.gar.vo.UserVO;

import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

@Repository
public class UserDaoImpl extends GenericDaoImpl<User, Long, UserVO> implements
		UserDao {

	private static final long serialVersionUID = 4669332217227001642L;

	@Override
	public User findByName(String username) {
		Query query = this.em
				.createQuery("select u FROM User u where u.username= :username");
		query.setParameter("username", username);
		List<User> users = query.getResultList();
		if (users != null && users.size() == 1) {
			return users.get(0);
		}
		return null;
	}

	@Override
	public User toEntityImpl(User entity, UserVO vo) {
		entity.setUsername(vo.getUsername());
		entity.setPassword(vo.getPassword());
		entity.setFirstName(vo.getFirstName());
		entity.setLastName(vo.getLastName());
		entity.setEmail(vo.getEmail());
		return entity;
	}

	@Override
	public UserVO toVOImpl(UserVO vo, User entity) {
		vo.setUsername(entity.getUsername());
		vo.setPassword(entity.getPassword());
		vo.setFirstName(entity.getFirstName());
		vo.setLastName(entity.getLastName());
		vo.setEmail(entity.getEmail());
		return vo;
	}

}
